//
//  JXLoginRegistCheckController.m
//  JiaoX
//
//  Created by jiaoxiu－005 on 15/1/12.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "JXLoginRegistCheckController.h"
#import "JXLoginController.h"
#import "JXRegisterController.h"
@interface JXLoginRegistCheckController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) JXLoginController *loginViewC;
@property (nonatomic, strong) JXRegisterController *registViewC;
@end

@implementation JXLoginRegistCheckController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置导航栏按钮
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImageName:@"navigationbar_back" highImageName:@"navigationbar_back_highlighted" target:self action:@selector(back)];
    //初始化登录和注册的子View
    [self initScrollViewWithChildView];
    //初始化切换导航
    [self initSegmentedControl];
}

/**
 *初始化登录和注册的子View
 */
- (void)initScrollViewWithChildView {
    //添加scrollView
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.frame = CGRectMake(0, 0, JXScreenW, self.view.height);
    [scrollView setContentSize:CGSizeMake(JXScreenW, self.view.height)];
    scrollView.showsVerticalScrollIndicator = FALSE;
    scrollView.showsHorizontalScrollIndicator = FALSE;
    [scrollView setBackgroundColor:[UIColor whiteColor]];
    scrollView.delegate = self;
    
    //添加登录和注册两个子View
    _loginViewC = [[JXLoginController alloc]init];
    UIView *loginV = _loginViewC.view;
    loginV.frame =  CGRectMake(0, 0, JXScreenW, self.view.height);
    [scrollView addSubview:loginV];
    
    _registViewC = [[JXRegisterController alloc]init];
    UIView *registV = _registViewC.view;
    registV.frame =  CGRectMake(0, 0, JXScreenW, self.view.height);
    [scrollView addSubview:registV];
   
    [loginV setHidden:NO];
    [registV setHidden:YES];
    
    _scrollView = scrollView;
    [self.view addSubview:scrollView];
}

/**
 *初始化切换导航
 */
- (void)initSegmentedControl
{
    NSArray *segmentedData = [[NSArray alloc]initWithObjects:@"登录",@"注册",nil];
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedData];
    segmentedControl.frame = CGRectMake(0, 0,120, 30.0);
    
    //这个是设置按下按钮时的颜色
    segmentedControl.tintColor = [UIColor whiteColor];
    segmentedControl.selectedSegmentIndex = 0;//默认选中的按钮索引登录
    [segmentedControl setBackgroundColor:[UIColor clearColor]];
    
    //下面的代码实同正常状态和按下状态的属性控制,比如字体的大小和颜色等
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:[DimensUtils getScaleX:14]],NSFontAttributeName,[UIColor whiteColor], NSForegroundColorAttributeName, nil];
    [segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];
    //设置高亮
    NSDictionary *selectedAttributes = [NSDictionary dictionaryWithObject:JXThemeColor forKey:NSForegroundColorAttributeName];
    [segmentedControl setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
    //设置分段控件点击相应事件
    [segmentedControl addTarget:self action:@selector(doSomethingInSegment:)forControlEvents:UIControlEventValueChanged];
    //设置layer
    CALayer *layer=[segmentedControl layer];
    [layer setCornerRadius:segmentedControl.height/2];
    //是否设置边框以及是否可见
    [layer setMasksToBounds:YES];
    //设置边框线的宽
    [layer setBorderWidth:[DimensUtils getScaleX:1]];
    //设置边框线的颜色
    [layer setBorderColor:[[UIColor whiteColor] CGColor]];
    //设置titleView
    self.navigationItem.titleView = segmentedControl;
}

/**
 * title View 切换点击
 */
-(void)doSomethingInSegment:(UISegmentedControl *)Seg
{
    NSInteger index = Seg.selectedSegmentIndex;
    //根据下标判断
    switch (index)
    {
        case 0://登录
            JXLog(@"登录");
            [_loginViewC.view setHidden:NO];
            [_registViewC.view setHidden:YES];
            break;
        case 1://注册
            JXLog(@"注册");
            [_loginViewC.view setHidden:YES];
            [_registViewC.view setHidden:NO];
            break;
        default:
            break;
    }
}

/**
 *  重写scrollview的滚动方法隐藏软件盘，结束输入,成为scrollview的代理才可以使用这个方法
 *
 *  @param scrollView
 */
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.view.superview endEditing:YES];
}

/**
 *  返回按钮
 */
-(void)back{
    [self dismissViewControllerAnimated:NO completion:^{
    }];
}

@end
